package view.listeners;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import model.Constants;
import view.views.dialogContentViews.AddSimpleIPContentPanel;
import controller.Calculator;

public class DecimalToBinaryListener extends FocusAdapter {


	private final AddSimpleIPContentPanel content;
	private String text;
	private final int i;

	public DecimalToBinaryListener(
			AddSimpleIPContentPanel addSimpleIPContentPanel, int i) {
		content = addSimpleIPContentPanel;
		this.i = i;
	}

	@Override
	public void focusLost(FocusEvent e) {
		text = content.getIpTextArea().getTextList().get(i).getText();
		if (text.equals(Constants.EMPTY) || text == null) {
			content.getBinaryIpTextArea().getTextList().get(i)
					.setText(Constants.EMPTY);

		} else {
			String binary = Calculator.getInstance().convertDecimalToBinary(
					Integer.parseInt(text));
			content.getBinaryIpTextArea()
					.getTextList()
					.get(i)
					.setText(binary);
		}
	}

}
